/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package co.edu.sd.jxta.socket;

import co.edu.sd.jxta.IPeer;
import co.edu.sd.jxta.IPeerUI;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import net.jxta.endpoint.Message;

/**
 *
 * @author SD
 */
public class CommonSocket {

    protected ObjectOutputStream output;
    protected ObjectInputStream input;
    protected int saveStateEvery = 100;
    protected IPeer peer;
    protected IPeerUI peerUI;

    protected void writeMessage(Message msg) throws IOException {
        output.writeObject(msg);
        output.flush();
    }

    protected Message readMessage() throws IOException, ClassNotFoundException {
        Message msg = (Message) input.readObject();
        /**SEC message and state here*/

        return msg;
    }

    protected boolean hasToSaveState(int i, int total) {
        return (i % saveStateEvery == 0 && i != 0) || i == total - 1;
    }

    protected void setStreams(Socket socket) throws IOException {
        output = new ObjectOutputStream(socket.getOutputStream());
        output.flush();
        input = new ObjectInputStream(socket.getInputStream());
    }
     

}
